package com.fingard.dsp.bank.directbank.gzelink01;

import java.net.URLEncoder;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.directbank.gzelink01.util.Md5;
import com.fingard.dsp.bank.directbank.gzelink01.util.PayUtils;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

public class GZELINK01QRPay extends GZELINK01Base implements IQRPay {
	public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
		RetAtsGenQRInfo retAts = new RetAtsGenQRInfo();
		
		retAts.rdSeq = p_req.rdSeq;
		retAts.respCode = "0";
		retAts.respInfo = reqHeader.bankCode + "无此接口";
		retAts.qrInfo = "";

		return retAts;
	}

	public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
		RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
		retAts.rdSeq = p_req.rdSeq;

		try {
			ActSetItem actItem = getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "");
			if (actItem != null) {
				// 商户ID
				String merId = "";
				if (StringHelper.hasAnyChar(actItem.merchantId)) {
					merId = actItem.merchantId;
				}
				
				// 商户支付流水号
				String orderNo = "";
				if (StringHelper.hasAnyChar(p_req.rdSeq)) {
					 orderNo = p_req.rdSeq;
				}
				
				// 支付金额  (单位:元)
				String orderAmount = "";
				if (StringHelper.hasAnyChar(p_req.amount)) {
					orderAmount = p_req.amount;
				}
				
				// 支付币种
				String currCode = "";
				if (StringHelper.hasAnyChar(p_req.cur)) {
					currCode = p_req.cur;
				}
				
				// 订单类型
				String orderType = "";
				
				// 通知商户网站支付结果的 url地址
				String callBackUrl = "";
				if (StringHelper.hasAnyChar(actItem.notifyURL)) {
					callBackUrl = actItem.notifyURL;
				}
				
				// 银行代码(直通车)
				String bankCode = "88978888";
				
				// 编码语言
				String langType = "UTF-8";
				
				// 业务类型
				String buzType = "19";
				
				// 保留域1
				String reserved01 = "";
				if (StringHelper.hasAnyChar(p_req.description)) {
					reserved01 = "DeviceInfo=MOBILE|Body=" + p_req.description + "|MemberId=|CounterNo=";
				}
				
				// 保留域2
				String reserved02 = "";
				
				// 签名数据
				String signMsg = "";
				
				//组装订单原始数据
				StringBuilder signData = new StringBuilder();
				
				signData.append(merId);
				signData.append(orderNo);
				signData.append(orderAmount);
				signData.append(currCode);
				signData.append(orderType);
				signData.append(callBackUrl);
				signData.append(bankCode);
				signData.append(langType);
				signData.append(buzType);
				signData.append(reserved01);
				signData.append(reserved02);
				
				// 执行签名
				String encodePKey = Md5.md5(actItem.saltKey, langType);
				signData.append(encodePKey);
				
				WriteBankLogLn2("广银联一码付获取二维码签名原始数据:\r\n" + signData.toString());
				
				signMsg = Md5.md5(signData.toString(), langType);
				
				//组装POST请求参数
				StringBuilder reqSendData = new StringBuilder();
				
				reqSendData.append("MerId=").append(merId);
				reqSendData.append("&OrderNo=").append(orderNo);
				reqSendData.append("&OrderAmount=").append(orderAmount);
				reqSendData.append("&CurrCode=").append(currCode); 
				reqSendData.append("&OrderType=").append(orderType);
				reqSendData.append("&CallBackUrl=").append(URLEncoder.encode(callBackUrl,langType));
				reqSendData.append("&BankCode=").append(bankCode);
				reqSendData.append("&LangType=").append(langType);
				reqSendData.append("&BuzType=").append(buzType);
				reqSendData.append("&Reserved01=").append(URLEncoder.encode(reserved01,langType));
				reqSendData.append("&Reserved02=").append(reserved02);
				reqSendData.append("&SignMsg=").append(signMsg);
				
				WriteBankLogLn2("2.广银联一码付获取二维码请求数据:\r\n" + reqSendData.toString());
				
				String url = bankFront.getUrl("gatewayPay");
				WriteBankLogLn2("广银联一码付获取二维码服务地址:" + url);
				
				// 发送广银联
				String[] tmpRetStr = sendDataToGZE(reqSendData.toString(), url);
				
				// 解析广银联返回
				if(tmpRetStr[0].length() == 0){
					WriteBankLogLn2("3.广银联一码付获取二维码响应数据:\r\n" + tmpRetStr[1]);
					
					if (tmpRetStr[1].length() > 0) {
						if (tmpRetStr[1].indexOf("&") > 0) {
							String respCode = PayUtils.GetValue(tmpRetStr[1], "RespCode");
							if("0000".equals(respCode)) {
								retAts.respCode = "0"; 
								retAts.respInfo = PayUtils.GetValue(tmpRetStr[1], "RespMsg");
								
								String orderNO = PayUtils.GetValue(tmpRetStr[1], "OrderNo");
								if (StringHelper.hasAnyChar(orderNO)) {
									retAts.rdSeq = orderNO;
								}
								
								String qrInfo = PayUtils.GetValue(tmpRetStr[1], "Reserved02");
								if (StringHelper.hasAnyChar(qrInfo)) {
									retAts.qrInfo = qrInfo.substring(qrInfo.indexOf("=")+1);
								}
								
								retAts.transState = "success";
								retAts.payInfoCode = respCode;
							} else {
								retAts.respCode = "-1"; 
								retAts.respInfo = PayUtils.GetValue(tmpRetStr[1], "RespMsg");
							}
						} else {
							retAts.respCode = "-1"; 
							retAts.respInfo = "广银联返回数据有误,请联系广银联相关技术人员!";
						}
					} else {
						retAts.respCode = "-1"; 
						retAts.respInfo = "广银联返回内容为空!";
					}
				} else {
					retAts.respCode = "-1"; 
					retAts.respInfo = tmpRetStr[1];
				}
			} else {
				retAts.respCode = "0";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
				retAts.qrInfo = "";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retAts;
	}

	public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
		RetAtsQueryOrder retAts = new RetAtsQueryOrder();
		retAts.rdSeq = p_req.rdSeq;

		try {
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "");
			if (actItem != null) {
				String tranType = "100";
				String javaCharset = "UTF-8";
				String version = "V60";
				String userId = actItem.userCode;
				String pwd = Md5.md5(actItem.userFlag, "UTF-8");
				String merId = actItem.merchantId;
				String payStatus = "2";
				String shoppingTime = Format.Date10Format.format(new Date()) + " 00:00:00";
				String beginTime = "";
				String endTime = "";
				String orderNo = p_req.rdSeq;
				
				//组装请求参数
				StringBuilder reqSendData = new StringBuilder();
				
				reqSendData.append("TranType=").append(tranType);
				reqSendData.append("&JavaCharset=").append(javaCharset);
				reqSendData.append("&Version=").append(version);
				reqSendData.append("&UserId=").append(userId); 
				reqSendData.append("&Pwd=").append(pwd);
				reqSendData.append("&MerId=").append(merId);
				reqSendData.append("&PayStatus=").append(payStatus);
				reqSendData.append("&ShoppingTime=").append(shoppingTime);
				reqSendData.append("&BeginTime=").append(beginTime);
				reqSendData.append("&EndTime=").append(endTime);
				reqSendData.append("&OrderNo=").append(orderNo);
				
				WriteBankLogLn2("2.广银联一码付下单查询请求数据:" + reqSendData.toString());
				
				String url = bankFront.getUrl("gatewayPayQuery");
				WriteBankLogLn2("广银联一码付下单查询服务地址:" + url);
				
				String[] tmpRetStr = sendDataToGZE(reqSendData.toString(), url);
				
				if(tmpRetStr[0].length() == 0) { 
					WriteBankLogLn2("3.广银联一码付下单查询响应数据:\r\n" + tmpRetStr[1]);
					
					if (tmpRetStr[1].length() > 0) {
						if (tmpRetStr[1].indexOf("&") > 0) {
							String[] respDetail = tmpRetStr[1].split("&");
							
							retAts.respCode = respDetail[0].split("=")[1];
							retAts.respInfo = respDetail[1].split("=")[1];
						} else {
							String[] respDetail = tmpRetStr[1].split("\\\\n");
							
							retAts.respCode = "0";
							retAts.respInfo = "";
							
							String rdSeq = respDetail[2];
							if (StringHelper.hasAnyChar(rdSeq)) {
								retAts.rdSeq = rdSeq;
							} else {
								retAts.rdSeq = p_req.rdSeq;
							}
							
							String status = respDetail[5];
							if (StringHelper.hasAnyChar(status)) {
								retAts.transState = respDetail[5];
								retAts.payInfoCode = respDetail[5];
							} else {
								// 未扫码查询不会返回状态，此时状态是等待买家付款
								retAts.transState = "4";
								retAts.payInfoCode = "WAIT_PAY";
							}
						}
					} else {
						retAts.respCode = "-1"; 
						retAts.respInfo = "广银联返回内容为空!";
					}
				} else {
					retAts.respCode = "-1"; 
					retAts.respInfo = PayUtils.GetValue(tmpRetStr[1], "Message");
				}
			} else {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}
		
		return retAts;
	}
}
